package chapter1;

import lombok.extern.slf4j.Slf4j;

/**
 * @author caiya
 * @date 2020/3/13 20:53
 */
@Slf4j
public class ThreadYieldTest implements Runnable {

    public static void main(String[] args) {
        /**
         * 演示线程yield用法，出让CPU使用权，使得其它线程得以被调度
         */
        new Thread(new ThreadYieldTest()).start();
        new Thread(new ThreadYieldTest()).start();
    }

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            log.debug("线程 - {}: {}", Thread.currentThread().getName(), i);
            if (i % 5 == 0) {
                Thread.yield();
            }
        }
        log.debug("线程 - {} is over", Thread.currentThread().getName());
    }
}
